<template>
  <div class='sign-daily'>
    <header class='header-con header-w-bg'>
      <router-link to='/membercenter'>
        <a>
          <i class='back-icon'></i>
        </a>
      </router-link>
      <h1 class='header-tit'>签到</h1>
    </header>
    <div class='my-page'>
      <div class='myCheckin-con'>
        <div class='myCheckin-hd'>
          <div class='my-scores'>我的积分：<span v-cloak>{{ userScore.content.totalPoints }}</span></div>
          <div :class="['myCheckin-warn', signInMind ? 'active' : '']">签到提醒<span @click="changeSignMind"><i></i></span></div>
        </div>
        <div class='myCheckin-bd'>
          <div :class="['myCheckin-status', parseInt(todaySign.content)==0 ? 'hasCheckin' : '']">
            <img src='../../assets/image/membercenter/signin.png' alt='' @click="signIn">
            <div class='hasCheckin-con'>
              <p>已连续签到</p>
              <h2><span>{{ userScore.content.continuitySignin }}</span>天</h2>
            </div>
          </div>
          <div class='myCheckin-tip'>
            <div v-cloak v-if="!parseInt(todaySign.content)==0">今日签到可领<span>{{ userScore.content.pointsToDay }}</span>积分</div>
            <div v-cloak v-if="parseInt(todaySign.content)==0">明天签到可领<span>{{ userScore.content.pointsTomDay }}</span>积分</div>
            <p>连续签到有更多惊喜哦~</p>
          </div>
        </div>
      </div>
      <div class='myCheckin-suc' v-if="signSuccess">
        <div>
          <img src='../../assets/image/membercenter/checkinSuc.png' alt=''>
          <h3 title='+20'>+<span>{{ userScore.content.pointsToDay }}</span></h3>
          <i @click="closePopup"></i>
        </div>
      </div>
      <sign-calendar :datelist='dateList'></sign-calendar>
    </div>
  </div>
</template>

<script>
  import axios from 'axios'
  import qs from 'qs'
  import weui from 'weui.js'
  import common from '../../assets/js/common'
  import calendar from '../../assets/js/calendar/calendar'
  import signCalendar from '../../components/calendar/signCalendar'

  export default {
    data () {
      return {
        userScore: {
          'resultCode': '',
          'resultMsg': '',
          'content': {
            'totalPoints': '',
            'pointsToDay': '',
            'pointsTomDay': '',
            'continuitySignin': '',
            'signInTimes': []
          },
          'accessToken': null
        },
        dateList: [
          {
            year: '',
            month: '',
            date: '',
            week: '',
            isholiday: false,
            holidayname: '',
            able: false,
            ispicked: false
          }
        ],
        todaySign: {
          'resultCode': '',
          'resultMsg': '',
          'content': '',
          'accessToken': null
        },
        signSuccess: false,
        signInMind: false
      }
    },
    components: {
      signCalendar
    },
    created: function () {
      var self = this
      var memberId = common.memberid
      var config = {
        headers: {
          'access-token': common.accesstoken,
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }
      var data = qs.stringify({
        memberId: memberId
      })
      self.getSignInfo(data, config)
      self.getIsSign(data, config)
    },
    methods: {
      getSignInfo: function (data, config) {
        var queryUrl = common.api.memberCenter.getSignInfo
        var self = this
        var toady = new Date()
        var newdata = qs.parse(data)
        newdata.year = toady.getFullYear()
        newdata = qs.stringify(newdata)
        // 请求签到数据
        axios.post(queryUrl, newdata, config).then(function (res) {
          self.userScore = res.data
          var today = new Date()
          var datelistobj = calendar.getMonthDay([today])
          var datelist = []
          var signInTimes = self.userScore.content.signInTimes
          for (var i = 0; i < signInTimes.length; i++) {
            datelistobj[0][signInTimes[i]].ispick = true
          }
          for (var props in datelistobj[0]) {
            datelist.push(datelistobj[0][props])
          }
          self.dateList = datelist
        })
      },
      getIsSign: function (data, config) {
        var queryUrl = common.api.memberCenter.getIsSignIn
        var self = this
        // 请求是否签到
        axios.post(queryUrl, data, config).then(function (res) {
          self.todaySign = res.data
        })
      },
      signIn: function () {
        var self = this
        var queryUrl = common.api.memberCenter.signIn
        var memberId = common.memberid
        var config = {
          headers: {
            'access-token': common.accesstoken,
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }
        var data = qs.stringify({
          memberId: memberId
        })
        // 签到
        axios.post(queryUrl, data, config).then(function (res) {
          if (parseInt(res.data.content) === 1) {
            self.signSuccess = true
            self.getSignInfo(data, config)
            self.getIsSign(data, config)
          } else {
            weui.alert(res.data.resultMsg)
          }
        })
      },
      closePopup: function () {
        var self = this
        self.signSuccess = false
      },
      changeSignMind: function () {
        var self = this
        switch (self.signInMind) {
          case true:
            self.signInMind = !self.signInMind
            weui.toast('关闭成功')
            break
          case false:
            self.signInMind = !self.signInMind
            weui.toast('开启成功')
            break
        }
      }
    }
  }
</script>

<style lang='scss'>
  @import '../../assets/sass/member/scores';
  @import '../../assets/sass/member/member.scss';
</style>


